/*
 * Copyright (c)  2015~2020, xforceplus
 * All rights reserved.
 * Project:datacare-orm
 * Id: TestNullFunction.java   2020-10-04 22-56-46
 * Author: Evan
 */
package cn.datacare.orm.engine.function;

import cn.datacare.orm.engine.SQLParameter;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

import static cn.datacare.orm.constant.Constants.PARAM_PARAMS;
import static cn.datacare.orm.util.StringUtils.EMPTY;

/**
 * <p>
 * Title:TestNullFunction
 * </p>
 * <p>
 * Description: ，参考AbstractDBStyle.appendInsertTemplateValue
 * </p>
 * <p>
 * Copyright: 2015~2020
 * </p>
 * <p>
 * Company/Department: xforceplus
 * </p>
 *
 * @author Evan
 * <b>Creation Time:</b> 2020-10-04 22-56-46
 * @since V1.0
 */
public class TestNullFunction implements Function {
    /**
     * 日志
     */
    private static final Logger log = LoggerFactory.getLogger(TestNullFunction.class);

    @Override
    public Object call(Object[] paras, Context ctx) {
        Object arg = paras[0];
        String paraName = (String) paras[1];
        if (arg == null) {
            return EMPTY;
        }
        try {
            //多余的逗号将被trim去掉
            ctx.byteWriter.writeString("?,");
            List list = (List) ctx.getGlobal(PARAM_PARAMS);
            list.add(new SQLParameter(paraName, arg));
        } catch (IOException e) {
            log.warn(e.getMessage(), e);
        }
        return EMPTY;

    }
}
